package echat.future.cn.echat.my.adapter;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.my.bean.LabelBean;
import echat.future.cn.echat.my.utils.ToastUtils;

public class TagAdapter extends BaseQuickAdapter<LabelBean,BaseViewHolder> {

    private List<Integer> selecetPostions = new ArrayList<>();

    public List<String> getSelecetTags() {
        List<String> returneDatas = new ArrayList<>();
        for(int i =0;i<selecetPostions.size();i++){
            returneDatas.add(mData.get(selecetPostions.get(i)).getSign());
        }
        return returneDatas;
    }


    public String getSelecetIds() {
        StringBuffer sb = new StringBuffer();
        for(int i =0;i<selecetPostions.size();i++){
            sb.append(mData.get(selecetPostions.get(i)).getId()+",");
        }
        return sb.length()>1?sb.substring(0,sb.length()-1):"";
    }

    public TagAdapter(@Nullable List<LabelBean> data) {
        super(R.layout.item_tag,data);
        setOnItemClickListener((adapter, view, position) -> {
            if(selecetPostions.contains(position)){
                //删除
                selecetPostions.remove(Integer.valueOf(position));
                adapter.notifyDataSetChanged();
            }else{
                if(selecetPostions.size() == 2){
                    ToastUtils.toastMsg(mContext,"仅能选择两个哦");
                    return;
                }
                selecetPostions.add(position);
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
//        super.onBindViewHolder(holder, position);
        holder.setText(R.id.tag,mData.get(position).getSign())
                .setBackgroundRes(R.id.tag,getPostionBg(position));
    }

    @Override
    protected void convert(BaseViewHolder helper, LabelBean item) {
        helper.setText(R.id.tag,item.getSign())
//                .setTextColor(R.id.tag, Color.parseColor(item.getColor()))
                .setBackgroundRes(R.id.tag,
                getPostionBg(mData.indexOf(item)));
    }

    /**
     *
     * @return
     */
    private int getPostionBg(int postion){
        for(int i = 0;i<selecetPostions.size();i++){
            if(i == 0 && selecetPostions.get(i) == postion){
                return R.drawable.item_tag_selector1;
            }
            if(i == 1 && selecetPostions.get(i) == postion){
                return R.drawable.item_tag_selector2;
            }
        }
        return R.drawable.item_tag_normal;
    }
}
